﻿Imports System.Data.SqlClient
Public Class ConnectDatabase
    Dim common_conn As New common
    Public ConnString As String
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        If InstancenameTextBox.Text = "" Then
            MsgBox("Please Enter Instance Name")
        ElseIf UsernameTextBox.Text = "" Then
            MsgBox("Please Enter Username")
        ElseIf PasswordTextBox.Text = "" Then
            MsgBox("Password Cannot be blank")
        Else
            OK.Text = "Connecting..."
            OK.Enabled = False

            ConnString = "Network Library=DBMSSOCN; data source=" & InstancenameTextBox.Text & ";initial catalog=test_case_db;user id=" & UsernameTextBox.Text & ";password=" & PasswordTextBox.Text & ";pooling=false"
            ' MsgBox(ConnString)
            ' My.Settings("ConnStrings") = ConnString
            ' MsgBox(My.Settings.ConnStrings & "saddddddddddddddddd")


            ' Dashboard.Show()


            '  Me.Close()
            Dim abc
            abc = common_conn.CheckConnection(ConnString)

            If abc = "" Then
                SetRegistryValue("stringcon", ConnString)
                'My.Settings("ConnStrings") = ConnString
                Me.Hide()
                Dashboard_old.Show()
            Else
                MsgBox(abc)
                OK.Text = "&Test Connection"
                OK.Enabled = True

                'If Me.Visible = False Then
                '    Me.ShowDialog()

                'End If
            End If
        End If


    End Sub


    Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        '    Dim response As MsgBoxResult
        '    response = MsgBox("Do you want to cancel?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
        '    If response = MsgBoxResult.Yes Then
        '        Me.Dispose()
        '        test.Dispose()
        '    ElseIf response = MsgBoxResult.No Then
        '        e.Cancel = True
        '        test.Hide()
        '        Exit Sub
        '    End If
        Me.Hide()

    End Sub


    Public Sub SetRegistryValue(ByVal KeyName As String, ByVal _Value As Object)
        Try
            Dim k = My.Computer.Registry.CurrentUser.OpenSubKey("Software\TCGen", True)
            If k IsNot Nothing Then
                k.SetValue(KeyName, _Value)
            Else
                k = My.Computer.Registry.CurrentUser.CreateSubKey("Software\TCGEN")
                k.SetValue(KeyName, _Value)
            End If
            If k IsNot Nothing Then k.Close()
        Catch ' ex As Exception
            'PromptMsg(ex)
        End Try
    End Sub

    Public Function GetRegistryValue(ByVal KeyName As String, Optional ByVal DefaultValue As Object = Nothing) As Object
        Dim res As Object = Nothing
        Try
            Dim k = My.Computer.Registry.CurrentUser.OpenSubKey("Software\TCGen", True)
            If k IsNot Nothing Then
                res = k.GetValue(KeyName, DefaultValue)
            Else
                k = My.Computer.Registry.CurrentUser.CreateSubKey("Software\TCGen")
            End If
            If k IsNot Nothing Then k.Close()
        Catch ' ex As Exception
            'PromptMsg(ex)
        End Try
        Return res
    End Function
End Class
